package schema

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"higame/modules/utils"
)

type SetItemSchema struct {
	Addressee  string `json:"addressee" bson:"addressee"`
	BackupTime string `json:"backupTime" bson:"backupTime"`
	BackupPath string `json:"backupPath" bson:"backupPath"`
}
type SetSchema struct {
	ID     primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
	Notice string             `json:"notice" bson:"notice"`
	Set    SetItemSchema      `json:"set" bson:"set"`
}
type Set struct {
	bson bson.M
}

func NewSet(bson bson.M) *Set {
	return &Set{
		bson: bson,
	}
}

func (this *Set) Parse() SetSchema {
	if this.bson == nil {
		return SetSchema{}
	}
	resolve := utils.NewResolve(this.bson)
	setValue := resolve.T("set")
	resolveSet := utils.NewResolve(setValue.(bson.M))
	setSchema := SetItemSchema{
		Addressee:  resolveSet.String("addressee", ""),
		BackupTime: resolveSet.String("backupTime", ""),
		BackupPath: resolveSet.String("backupPath", ""),
	}

	return SetSchema{
		ID:     resolve.ObjectID("_id"),
		Notice: resolve.String("notice", ""),
		Set:    setSchema,
	}
}
